package com.feige.web.result;

import com.feige.base.vo.BaseVo;
import com.feige.constant.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author feige
 */
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "返回单条数据")
@Data
public class SingleResult<R extends BaseVo> extends BaseResult {
    private static final long serialVersionUID = -3787347346339735226L;

    @ApiModelProperty(value = "返回数据")
    private R data;

    public static  <R extends BaseVo> SingleResult<R> success(R data){
        SingleResult<R> result = new SingleResult<>();
        result.setSuccess();
        result.setData(data);
        return result;
    }

    public static  <R extends BaseVo> SingleResult<R> isOk(boolean r){
        if (r){
            return success();
        }else {
            return failure();
        }
    }

    public static <R extends BaseVo> SingleResult<R> empty(){
        SingleResult<R> success = success();
        success.setCode(Constants.NO_DATA_CODE);
        return success;
    }

    public static  <R extends BaseVo> SingleResult<R> failure(R data){
        SingleResult<R> result = new SingleResult<>();
        result.setFailure();
        result.setData(data);
        return result;
    }

    public static <R extends BaseVo> SingleResult<R> exception(Exception e){
        SingleResult<R> result = new SingleResult<>();
        result.setException(e);
        return result;
    }

    public static <R extends BaseVo> SingleResult<R> noAuth(){
        SingleResult<R> result = new SingleResult<>();
        result.setNoAuth();
        return result;
    }

    public static <R extends BaseVo> SingleResult<R> success(){
        return success(null);
    }

    public static <R extends BaseVo> SingleResult<R> failure(){
        return failure(null);
    }
    
}
